﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using Frame.Lib.Core.User;
using Frame.Lib.Core.Role;
using Frame.Lib.Utilities;

public partial class Admin_User_SaveRole : System.Web.UI.Page
{
    RoleBO role
    {
        get
        {
            return RoleBO.Instance<RoleBO>();
        }
    }
    UserBO user
    {
        get
        {
            return UserBO.Instance<UserBO>();
        }
    }
    public int UserID
    {
        get
        {
            return int.Parse(StrUtil.DecryptUrl(Request["UserID"]));
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindTree();
            
            //_selectRoleIDs = user.GetRoleIDs(UserID);

            //string strSelectRoleIDs = StrUtil.MergeToStr(lstSelectRoleIDs, ",");
            //if (lstSelectRoleIDs.Count > 0)
            //{
            //    strSelectRoleIDs = "," + strSelectRoleIDs + ",";
            //}
            //hidSelectIDs.Value = strSelectRoleIDs;//已选择的角色放到隐藏域里

            //BindView();
            //lblUserName.Text = user.GetName(UserID);
            lblName.Text = user.GetName(UserID);
        }
        //else
        //{
        //    strSelectIDs = hidSelectIDs.Value;
        //    hidSelectIDs.Value = "";
        //}
    }

    private void BindTree()
    {
        List<int> roleIDs = user.GetRoleIDs(UserID);//还原用户已经选择的角色
        tvRole.CssClass = "treeview";
        tvRole.ShowLines = true;
        IEnumerable<RoleInfo> list = role.GetList();
        foreach (RoleInfo info in list)
        {
            TreeNode node = new TreeNode(info.Name, info.ID.ToString());
            node.ShowCheckBox = true;
            node.SelectAction = TreeNodeSelectAction.None;
            if (roleIDs.Contains(info.ID))
            {
                node.Checked = true;
            }
            tvRole.Nodes.Add(node);
            
        }
    }

    //private void BindView()
    //{
    //    GVUtil.InitGV(gvList);       
    //    IEnumerable<RoleInfo> list = role.GetList();
    //    gvList.DataSource = list;
    //    gvList.DataKeyNames = new string[] { "ID" };
    //    gvList.DataBind();
    //}
    //protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
    //{
    //    if (e.Row.RowType == DataControlRowType.DataRow)
    //    {
    //        int id = (int)gvList.DataKeys[e.Row.RowIndex]["ID"];
    //        if (lstSelectRoleIDs.IndexOf(id) >= 0)
    //        {
    //            CheckBox chkSelect = (CheckBox)e.Row.FindControl("chkSelect");
    //            chkSelect.Checked = true;
    //            e.Row.CssClass = "selected";
    //            e.Row.Attributes.Add("onmouseover", "null");
    //            e.Row.Attributes.Add("onmouseout", "null");
    //        }
    //    }
    //}
    protected void btnSave_Click(object sender, EventArgs e)
    {
        List<int> lstRoleIDs = new List<int>();
        foreach (TreeNode node in tvRole.CheckedNodes)
        {
            lstRoleIDs.Add(int.Parse(node.Value));
        }
        role.SaveUserIDs(lstRoleIDs, UserID);
        string strScript = "window.close();returnValue=true";
        JSUtil.ResponseJS(strScript);
    }   
}
